
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mytools;

import com.opensymphony.xwork2.ActionSupport;
import java.util.*;

/**

 *

 * @author xiaoxiao

 */
public class examAuto extends ActionSupport {

    private int eid;
    private String subject;
    private String[] chapter;
    private String danxuan;
    private String duoxuan;
    private String judge;
    private String question;
    private examDAO ed;
    private List<choice> autodanxuan;
    private List<choice> autoduoxuan;
    private List<judge> autojudge;
    private List<question> autoquestion;
    private exam exam;

    public examAuto() {
    }

    public String execute() throws Exception {
        setExam(new exam());
        setEd(new examDAO());
        exam = getEd().getexam(getEid());
        String chap;
        String result = "";
        String seprater = ",";
        String danxuanid = "";
        String duoxuanid = "";
        String judgeid = "";
        String questionid = "";
        String danxuanids;
        String duoxuanids;
        String judgeids;
        String questionids;
        if (getChapter() == null) {
            int danamount = getEd().getdanxuanAmount(getSubject());
            int duoamount = getEd().getduoxuanAmount(getSubject());
            int jamount = getEd().getjudgeAmount(getSubject());
            int qamount = getEd().getquestionAmount(getSubject());

            if (Integer.parseInt(getDanxuan()) >= danamount) {
                setAutodanxuan(getEd().getdanxuansubject(getSubject()));
            } else {
                int array[] = getEd().getdanxuansujectid(getSubject());
                if (array.length != 0) {
                    Random r = new Random();
                    HashSet set = new HashSet();
                    int s = 0;
                    while (s < Integer.parseInt(getDanxuan())) {
                        if (set.add(array[r.nextInt(array.length)])) {
                            s++;
                        }
                    }
                    Iterator<Integer> it = set.iterator();
                    while (it.hasNext()) {
                        danxuanid += it.next().toString() + seprater;
                    }
                    danxuanids = danxuanid.substring(0, danxuanid.length() - 1);
                    setAutodanxuan(getEd().getdanxuan(danxuanids));
                }
            }

            if (Integer.parseInt(getDuoxuan()) >= duoamount) {
                setAutoduoxuan(getEd().getduoxuansubject(getSubject()));
            } else {
                int array[] = getEd().getduoxuansujectid(getSubject());
                if (array.length != 0) {
                    Random r = new Random();
                    HashSet set = new HashSet();
                    int s = 0;
                    while (s < Integer.parseInt(getDuoxuan())) {
                        if (set.add(array[r.nextInt(array.length)])) {
                            s++;
                        }
                    }
                    Iterator<Integer> it = set.iterator();
                    while (it.hasNext()) {
                        duoxuanid += it.next().toString() + seprater;
                    }
                    duoxuanids = duoxuanid.substring(0, duoxuanid.length() - 1);
                    setAutoduoxuan(getEd().getduoxuan(duoxuanids));
                }
            }

            if (Integer.parseInt(getJudge()) >= jamount) {
                setAutojudge(getEd().getjudgesubject(getSubject()));
            } else {
                int array[] = getEd().getjudgesujectid(getSubject());
                if (array.length != 0) {
                    Random r = new Random();
                    HashSet set = new HashSet();
                    int s = 0;
                    while (s < Integer.parseInt(getJudge())) {
                        if (set.add(array[r.nextInt(array.length)])) {
                            s++;
                        }
                    }
                    Iterator<Integer> it = set.iterator();
                    while (it.hasNext()) {
                        judgeid += it.next().toString() + seprater;
                    }
                    judgeids = judgeid.substring(0, judgeid.length() - 1);
                    setAutojudge(getEd().getjudge(judgeids));
                }
            }

            if (Integer.parseInt(getQuestion()) >= qamount) {
                setAutoquestion(getEd().getquestionsubject(getSubject()));
            } else {
                int array[] = getEd().getquestionsujectid(getSubject());
                if (array.length != 0) {
                    Random r = new Random();
                    HashSet set = new HashSet();
                    int s = 0;
                    while (s < Integer.parseInt(getQuestion())) {
                        if (set.add(array[r.nextInt(array.length)])) {
                            s++;
                        }
                    }
                    Iterator<Integer> it = set.iterator();
                    while (it.hasNext()) {
                        questionid += it.next().toString() + seprater;
                    }
                    questionids = questionid.substring(0, questionid.length() - 1);
                    setAutoquestion(getEd().getquestion(questionids));
                }
            }

        } else {

            for (int i = 0; i < getChapter().length; i++) {
                result += getChapter()[i] + seprater;
            }
            chap = result.substring(0, result.length() - 1);

            int danamount = getEd().getdanxuanAmount(getSubject(), chap);
            int duoamount = getEd().getduoxuanAmount(getSubject(), chap);
            int jamount = getEd().getjudgeAmount(getSubject(), chap);
            int qamount = getEd().getquestionAmount(getSubject(), chap);

            if (Integer.parseInt(getDanxuan()) >= danamount) {
                setAutodanxuan(getEd().getdanxuansubjectchapter(getSubject(), chap));
            } else {
                int array[] = getEd().getdanxuansujectchapterid(getSubject(), chap);
                if (array.length != 0) {
                    Random r = new Random();
                    HashSet set = new HashSet();
                    int s = 0;
                    while (s < Integer.parseInt(getDanxuan())) {
                        if (set.add(array[r.nextInt(array.length)])) {
                            s++;
                        }
                    }
                    Iterator<Integer> it = set.iterator();
                    while (it.hasNext()) {
                        danxuanid += it.next().toString() + seprater;
                    }
                    danxuanids = danxuanid.substring(0, danxuanid.length() - 1);
                    setAutodanxuan(getEd().getdanxuan(danxuanids));
                }
            }

            if (Integer.parseInt(getDuoxuan()) >= duoamount) {
                setAutoduoxuan(getEd().getduoxuansubjectchapter(getSubject(), chap));
            } else {
                int array[] = getEd().getduoxuansujectchapterid(getSubject(), chap);
                if (array.length != 0) {
                    Random r = new Random();
                    HashSet set = new HashSet();
                    int s = 0;
                    while (s < Integer.parseInt(getDuoxuan())) {
                        if (set.add(array[r.nextInt(array.length)])) {
                            s++;
                        }
                    }
                    Iterator<Integer> it = set.iterator();
                    while (it.hasNext()) {
                        duoxuanid += it.next().toString() + seprater;
                    }
                    duoxuanids = duoxuanid.substring(0, duoxuanid.length() - 1);
                    setAutoduoxuan(getEd().getduoxuan(duoxuanids));
                }
            }

            if (Integer.parseInt(getJudge()) >= jamount) {
                setAutojudge(getEd().getjudgesubjectchapter(getSubject(), chap));
            } else {
                int array[] = getEd().getjudgesujectchapterid(getSubject(), chap);
                if (array.length != 0) {
                    Random r = new Random();
                    HashSet set = new HashSet();
                    int s = 0;
                    while (s < Integer.parseInt(getJudge())) {
                        if (set.add(array[r.nextInt(array.length)])) {
                            s++;
                        }
                    }
                    Iterator<Integer> it = set.iterator();
                    while (it.hasNext()) {
                        judgeid += it.next().toString() + seprater;
                    }
                    judgeids = judgeid.substring(0, judgeid.length() - 1);
                    setAutojudge(getEd().getjudge(judgeids));
                }
            }

            if (Integer.parseInt(getQuestion()) >= qamount) {
                setAutoquestion(getEd().getquestionsubjectchapter(getSubject(), chap));
            } else {
                int array[] = getEd().getquestionsujectchapterid(getSubject(), chap);
                if (array.length != 0) {
                    Random r = new Random();
                    HashSet set = new HashSet();
                    int s = 0;
                    while (s < Integer.parseInt(getQuestion())) {
                        if (set.add(array[r.nextInt(array.length)])) {
                            s++;
                        }
                    }
                    Iterator<Integer> it = set.iterator();
                    while (it.hasNext()) {
                        questionid += it.next().toString() + seprater;
                    }
                    questionids = questionid.substring(0, questionid.length() - 1);
                    setAutoquestion(getEd().getquestion(questionids));
                }
            }

        }
        return SUCCESS;
    }

    /**
     * @return the eid
     */
    public int getEid() {
        return eid;
    }

    /**
     * @param eid the eid to set
     */
    public void setEid(int eid) {
        this.eid = eid;
    }

    /**
     * @return the subject
     */
    public String getSubject() {
        return subject;
    }

    /**
     * @param subject the subject to set
     */
    public void setSubject(String subject) {
        this.subject = subject;
    }

    /**
     * @return the chapter
     */
    public String[] getChapter() {
        return chapter;
    }

    /**
     * @param chapter the chapter to set
     */
    public void setChapter(String[] chapter) {
        this.chapter = chapter;
    }

    /**
     * @return the danxuan
     */
    public String getDanxuan() {
        return danxuan;
    }

    /**
     * @param danxuan the danxuan to set
     */
    public void setDanxuan(String danxuan) {
        this.danxuan = danxuan;
    }

    /**
     * @return the duoxuan
     */
    public String getDuoxuan() {
        return duoxuan;
    }

    /**
     * @param duoxuan the duoxuan to set
     */
    public void setDuoxuan(String duoxuan) {
        this.duoxuan = duoxuan;
    }

    /**
     * @return the judge
     */
    public String getJudge() {
        return judge;
    }

    /**
     * @param judge the judge to set
     */
    public void setJudge(String judge) {
        this.judge = judge;
    }

    /**
     * @return the question
     */
    public String getQuestion() {
        return question;
    }

    /**
     * @param question the question to set
     */
    public void setQuestion(String question) {
        this.question = question;
    }

    /**
     * @return the ed
     */
    public examDAO getEd() {
        return ed;
    }

    /**
     * @param ed the ed to set
     */
    public void setEd(examDAO ed) {
        this.ed = ed;
    }

    /**
     * @return the autodanxuan
     */
    public List<choice> getAutodanxuan() {
        return autodanxuan;
    }

    /**
     * @param autodanxuan the autodanxuan to set
     */
    public void setAutodanxuan(List<choice> autodanxuan) {
        this.autodanxuan = autodanxuan;
    }

    /**
     * @return the autoduoxuan
     */
    public List<choice> getAutoduoxuan() {
        return autoduoxuan;
    }

    /**
     * @param autoduoxuan the autoduoxuan to set
     */
    public void setAutoduoxuan(List<choice> autoduoxuan) {
        this.autoduoxuan = autoduoxuan;
    }

    /**
     * @return the autojudge
     */
    public List<judge> getAutojudge() {
        return autojudge;
    }

    /**
     * @param autojudge the autojudge to set
     */
    public void setAutojudge(List<judge> autojudge) {
        this.autojudge = autojudge;
    }

    /**
     * @return the autoquestion
     */
    public List<question> getAutoquestion() {
        return autoquestion;
    }

    /**
     * @param autoquestion the autoquestion to set
     */
    public void setAutoquestion(List<question> autoquestion) {
        this.autoquestion = autoquestion;
    }

    /**
     * @return the exam
     */
    public exam getExam() {
        return exam;
    }

    /**
     * @param exam the exam to set
     */
    public void setExam(exam exam) {
        this.exam = exam;
    }
}
